using NL;
using NL.Advanced;
using System.Collections.Generic;
using UnityEngine;
namespace Miya.EmisssionLauncher
{
    public interface ILaunchAttackerHandler
    {
        List<string> GetAttackerStrArray();
    }
    public class LaunchingLoop_Attacker : MonoBehaviour, ILaunchingLoop, ILaunchAttackerHandler
    {


        [Header("供给部列表; 是否自动添加缺省供给部(type=power;data=0,0)")]
        public bool UseDefaultAttacker = true;

        public List<string> AttackerInitStrArr = new List<string>();

        public List<string> AdditionalAttackerInitStrArr = new List<string>();

        protected static string _DEFAULT_ATTACKER_INIT => $"type=power{NLConst.CHAR_ARRAY}data=0{NLConst.CHAR_INNER}0";


        protected  ICombatOwner _CombatOwner;
        protected virtual void Start()
        {
            _CombatOwner = _CombatOwner??GetComponentInParent<ICombatOwner>();
            var Info = gameObject.INFO();

            if (Info == null) return;

            //需要加载战斗数据..

            ///读取 攻击部 1~16

            for (int i = 1; i <= 16; i++)
            {
                string str = Info[$"attacker{i}"];
                if (!string.IsNullOrEmpty(str))
                {
                    AddAttackerInitStr(str);
                }
            }
        }

        public LaunchingLoop_Attacker SetCombatOwner(ICombatOwner Owner)
        {
            _CombatOwner= Owner;
            return this;
        }

        public LaunchingLoop_Attacker AddAttackerInitStr(string str)
        {
            if (str.IndexOf("bomb") >= 0)
            {
                if (CombatSetting.LOGable)
                    LOG.Add($"[LaunchingLoop_Attacker.AddAttackerInitStr]..{str}");
            }
            AttackerInitStrArr?.Add(str);
            return this;
        }

        public LaunchingLoop_Attacker ClearAdditionalAttackerInitStr()
        {
            AdditionalAttackerInitStrArr?.Clear();
            return this;
        }

        public LaunchingLoop_Attacker AddAdditionalAttackerInitStr(string str)
        {
            if (str.IndexOf("bomb") >= 0)
            {
                if (CombatSetting.LOGable)
                    LOG.Add($"[LaunchingLoop_Attacker.AddAdditionalAttackerInitStr]..{str}");
            }
            AdditionalAttackerInitStrArr?.Add(str);
            return this;
        }

        void ILaunchingLoop.OnLoopBegin() { }
        void ILaunchingLoop.OnLoopEnd() { }
        void ILaunchingLoop.OnLoopOneBegin() { }
        void ILaunchingLoop.OnLoopOneEnd() { }


        void ILaunchingLoop.OnLoopOneBB(GameObject bb) => OnLoopOneBB(bb);

        protected virtual void OnLoopOneBB(GameObject bb)
        {
            //位子弹添加 战斗宿主
            bb.GetComponent<BB>()?.SetCombatOwner(_CombatOwner, 1);

            AddAttacker(bb);
        }

        protected virtual void AddAttacker(GameObject bb)
        {

            if (UseDefaultAttacker)
            {
                var newatt = bb.AddComponent<Attacker>().Set(_DEFAULT_ATTACKER_INIT).SetDisposable(true);
            }

            foreach (var a in AttackerInitStrArr)
            {
                var newatt = bb.AddComponent<Attacker>().Set(a).SetDisposable(true);

                if (CombatSetting.LOGable)
                    LOG.Add($"[LauncherWithCombat.OnLoopOneBB.AddComponent<Attacker>] :{a} ");
            }
            // LOG.Add($"[LauncherWithCombat.OnLoopOneBB].{_CombatOwner.gameObject}的技能{gameObject.name}发射子弹{one}");
        }

        List<string> ILaunchAttackerHandler.GetAttackerStrArray()
        {
            List<string> list = new List<string>();
            if(UseDefaultAttacker)
            {
                list.Add(_DEFAULT_ATTACKER_INIT);
            }
            list.AddRange(AttackerInitStrArr);
            return list;
        }
    }
}

